package development.bussiness;

import java.util.Vector;

import state.GameConstants;
import state.GameImage;
import state.font.FontManager;
import state.socket.DataPackage;
import state.socket.MapDataPackage;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.NumberUtil;
import state.util.array.ByteArray;
import state.util.array.StringArray;
import development.Constants;
import development.FrmNotification;
import development.Global;
import development.Utils;
import development.bean.Item;
import development.bean.Notification;

public class NotificationBusiness {

  private Notification read(DataPackage dataPackage) {
    MapDataPackage mapDataPackage = MapDataPackage.getMapPackage(dataPackage);
    Notification notification = new Notification();
    notification.code = mapDataPackage.getString("code");
    notification.name = mapDataPackage.getString("name");
    notification.module = mapDataPackage.getString("module");
    String formSize = mapDataPackage.getString("formSize");
    ByteArray btBgImage = mapDataPackage.getByteArray("bgImage");
    notification.bgImage = GameImage.createImage(btBgImage, 0, btBgImage.len());
    if (!formSize.equals("")) {
      StringArray arrFormSize = GameStringUtils.split(formSize, ",");
      notification.SIZE_X = NumberUtil.parseInt(arrFormSize.array[0]);
      notification.SIZE_Y = NumberUtil.parseInt(arrFormSize.array[1]);
    }
    String formPosition = mapDataPackage.getString("formPosition");
    if (!formPosition.equals("")) {
      String[] arrFormPosition = Utils.split(formPosition, ",");
      notification.POSITION_X = NumberUtil.parseInt(arrFormPosition[0]);
      notification.POSITION_Y = NumberUtil.parseInt(arrFormPosition[1]);
    }
    notification.timeAlive = mapDataPackage.getInt("timeAlive");
    int loopCount = mapDataPackage.getInt("loopCount");
    notification.items = new Vector();
    for (int i = 0; i < loopCount; i++) {
      Item item = new Item();
      item.type = mapDataPackage.getString("type_" + String.valueOf(i));
      item.content = mapDataPackage.getString("content_" + String.valueOf(i));
      if (item.type.equals("IMAGE")) {
        ByteArray btImage = mapDataPackage.getByteArray("image_" + String.valueOf(i));
        item.image = GameImage.createImage(btImage, 0, btImage.len());
      }
      String color = mapDataPackage.getString("color_" + String.valueOf(i));
      if (color.equals("WHITE_CODE")) {
        item.color = GameColor.WHITE_CODE;
      } else if (color.equals("BLACK_CODE")) {
        item.color = GameColor.BLACK_CODE;
      } else if (color.equals("YELLOW_CODE")) {
        item.color = GameColor.YELLOW_CODE;
      } else if (color.equals("GREEN_CODE")) {
        item.color = GameColor.GREEN_CODE;
      } else if (color.equals("BLUE_CODE")) {
        item.color = GameColor.BLUE_CODE;
      } else if (color.equals("DARK_BLUE_CODE")) {
        item.color = GameColor.DARK_BLUE_CODE;
      } else if (color.equals("RED_CODE")) {
        item.color = GameColor.RED_CODE;
      } else if (color.equals("RED2_CODE")) {
        item.color = GameColor.RED2_CODE;
      } else if (color.equals("DARK_BLUE_CODE")) {
        item.color = GameColor.ORANGE_CODE;
      } else {
        item.color = NumberUtil.parseInt(color);
        if (item.color == NumberUtil.ERROR_VALUE) {
          item.color = GameColor.BLACK_CODE;
        }
      }
      String fontSize = mapDataPackage.getString("fontSize_" + String.valueOf(i));
      item.imageText = FontManager.getFont(FontManager.FONT_SIZE_8);
      if (fontSize.equals("FONT_SIZE_13")) {
        item.imageText = FontManager.getFont(FontManager.FONT_SIZE_13);
      } else if (fontSize.equals("FONT_SIZE_11")) {
        item.imageText = FontManager.getFont(FontManager.FONT_SIZE_11);
      }
      item.fileDownloadUrl = mapDataPackage.getString("fileDownloadUrl_" + String.valueOf(i));
      if (Constants.IS_240x320_SCREEN) {
        item.screen_240x320_x = mapDataPackage.getInt("screen_240x320_x_" + String.valueOf(i));
        item.screen_240x320_y = mapDataPackage.getInt("screen_240x320_y_" + String.valueOf(i));
        if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("TOP_LEFT_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.TOP_LEFT_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("TOP_RIGHT_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.TOP_RIGHT_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("TOP_HCENTER_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.TOP_HCENTER_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("VCENTER_LEFT_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.VCENTER_LEFT_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("VCENTER_RIGHT_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.VCENTER_RIGHT_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("CENTER_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.CENTER_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("BOTTOM_HCENTER_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.BOTTOM_HCENTER_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("BOTTOM_LEFT_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.BOTTOM_LEFT_ANCHOR;
        } else if (mapDataPackage.getString("screen_240x320_x_" + String.valueOf(i)).equals("BOTTOM_RIGHT_ANCHOR")) {
          item.screen_240x320_anchor = GameConstants.BOTTOM_RIGHT_ANCHOR;
        }
      } else {
        item.screen_320x240_x = mapDataPackage.getInt("screen_320x240_x_" + String.valueOf(i));
        item.screen_320x240_y = mapDataPackage.getInt("screen_320x240_y_" + String.valueOf(i));
        if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("TOP_LEFT_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.TOP_LEFT_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("TOP_RIGHT_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.TOP_RIGHT_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("TOP_HCENTER_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.TOP_HCENTER_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("VCENTER_LEFT_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.VCENTER_LEFT_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("VCENTER_RIGHT_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.VCENTER_RIGHT_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("CENTER_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.CENTER_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("BOTTOM_HCENTER_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.BOTTOM_HCENTER_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("BOTTOM_LEFT_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.BOTTOM_LEFT_ANCHOR;
        } else if (mapDataPackage.getString("screen_320x240_x_" + String.valueOf(i)).equals("BOTTOM_RIGHT_ANCHOR")) {
          item.screen_320x240_anchor = GameConstants.BOTTOM_RIGHT_ANCHOR;
        }
      }
      item.forwardUrl = mapDataPackage.getString("forwardUrl_" + String.valueOf(i));
      item.index = mapDataPackage.getInt("index_" + String.valueOf(i));
      item.id = mapDataPackage.getInt("itemId_" + String.valueOf(i));
      notification.items.addElement(item);
    }
    return notification;
  }

  public void onReceiveNotification(DataPackage dataPackage) {
    Notification notification = read(dataPackage);
    StringArray classes = GameStringUtils.split(notification.module, ",");
    for (int j = 0; j < classes.len(); j++) {
      if (Global.systemCanvas.frmCurrent.getClass().getName().equals(classes.array[j])) {
        Hashtable parameters = new Hashtable();
        parameters.put("notification", notification);
        FrmNotification.instance().init(parameters);
        break;
      }
    }
  }
}
